package org.itboys.mobile.service.pos;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.json.JsonUtils;
import org.itboys.mobile.entity.pos.income.PosIncomeResult;
import org.itboys.mobile.entity.pos.member.PosMember;
import org.itboys.mobile.entity.pos.member.PosMemberResult;
import org.itboys.mobile.entity.pos.product.PosProduct;
import org.itboys.mobile.entity.pos.score.PosScoreResult;
import org.itboys.mobile.service.pos.util.PosHttpHelper;
import org.itboys.mobile.service.pos.util.PosMD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2016年7月8日
 * 联系方式 ：empty90911@126.com
 * 描述：花果山_pos_用户（含积分和余额）_
 */
@Service
public class PosMemberService {
	private final static Logger logger = LoggerFactory.getLogger(PosHttpHelper.class);
	
	public final static String score_transType_offset="OffSet";
	public final static String score_transType_Reward="Reward";

	/**
	 * 
	 * 查询单个会员信息（会返回余额和积分）
	 * 
	 * 会员卡状态：0未发卡,1已发卡,8挂失,9作废
	 * **/
	public  PosMember getPosMember(String CardNO) {

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("Method", "GetVipInfo");
		//时间搓
		Long timeStamp = new Date().getTime();
		param.put("TimeStamp", timeStamp);
		//	Integer Page =1;
		//参数
		String data = "{\"CardNO\":\"" + CardNO + "\"}";

		//param.put("data", data);
		//签名
		String Signature = PosMD5Util.MD5(PosCommons.token + timeStamp + data);
		param.put("data", data);
		param.put("Signature", Signature.toUpperCase());

		String posMemberResult = PosHttpHelper.doPost(PosCommons.url, param);
		logger.info("--pos-查询会员卡-结果"+posMemberResult);
		PosMemberResult result=  JsonUtils.jsonToObject(PosMemberResult.class, posMemberResult);
		Integer code = result.ResCode;
		if(code!=0){
			throw new ErrorException(result.Msg);
		}else{
			return result.ResData.get(0);
		}

		//return null;
	}

	/**
	 * 变更用户积分。增加或减少。都是正数。pos自动增减流水
	 * @param CardNO
	 * @param RealIntegral 积分数量
	 * @param TransType Reward 增加 OffSet减少
	 * @param VoucherNo
	 */
	public  void changePosScore(
			String CardNO,BigDecimal RealIntegral,
			String TransType,String VoucherNo
	){
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("Method", "VipIntegral");
		//时间搓
		Long timeStamp=new Date().getTime();
		param.put("TimeStamp", timeStamp);
		//String BranchCode="01001";
		BigDecimal ConsumAmt = new BigDecimal("0");
		String data = "{\"CardNO\":\"" +CardNO +
				"\",\"OperDate\":\"" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +
				"\",\"RealIntegral\":" + RealIntegral +
				",\"TransType\":\"" + TransType +
				"\",\"VoucherNo\":\"" + VoucherNo+
				"\",\"ConsumAmt\":" + ConsumAmt+
				/*",\"BranchCode\":\"" + BranchCode+*/
				"}";

		//签名
		String SignatureParam = PosCommons.token+timeStamp+data;
		String Signature =PosMD5Util.MD5(SignatureParam);
		param.put("data", data);
		param.put("Signature", Signature.toUpperCase());
		String posScoreResult= PosHttpHelper.doPost(PosCommons.url, param);
		logger.info("--pos-变更会员卡积分-结果"+posScoreResult);
		PosScoreResult result=  JsonUtils.jsonToObject(PosScoreResult.class, posScoreResult);
		if(result.ResCode!=0){
			throw new ErrorException(result.Msg);
		}


	}


	/*
	**
	 * 增加余额
	 * @param CardNO
	 * @param AddAmt
	 * @param PayAmt
	 */
	public   void addPosBalance(
			String CardNO,BigDecimal AddAmt,BigDecimal PayAmt
	){
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("Method", "VipCharge");
		//时间搓
		Long timeStamp=new Date().getTime();
		param.put("TimeStamp", timeStamp);
	
		String data = "{\"CardNO\":\"" +CardNO +
				"\",\"ChargeDate\":\"" +  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +
				"\",\"AddAmt\":" + AddAmt +
				",\"PayAmt\":" + PayAmt +
				"}";
		//签名
		String SignatureParam = PosCommons.token+timeStamp+data;
		//logger.info("签名参数---"+SignatureParam);
		String Signature =PosMD5Util.MD5(SignatureParam);
		//logger.info("签名---"+Signature);
		param.put("data", data);
		param.put("Signature", Signature.toUpperCase());

		String posAddBalanceResult= PosHttpHelper.doPost(PosCommons.url, param);
		logger.info("--pos-增加用户余额-结果"+posAddBalanceResult);

		PosIncomeResult result=  JsonUtils.jsonToObject(PosIncomeResult.class, posAddBalanceResult);
		Integer code = result.ResCode;
		if(code!=0){
			throw new ErrorException(result.Msg);
		}

	}
	/**
	 * 扣减余额
	 * @param CardNO
	 * @param PayAmt
	 * @param VoucherNo
	 */
	public    void deductionPosBalance(String CardNO,BigDecimal PayAmt,String VoucherNo){
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("Method", "VipPay");
		//时间搓
		Long timeStamp=new Date().getTime();
		param.put("TimeStamp", timeStamp);
		String Pwd="12345";//默认密码
		String BranchCode="";
		String data = "{\"CardNO\":\"" +CardNO +
				"\",\"PayDate\":\"" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +
				"\",\"PayAmt\":" + PayAmt +
				",\"VoucherNo\":\"" + VoucherNo +
				"\",\"BranchCode\":\"" + BranchCode +
				"\",\"Pwd\":\"" + Pwd +
				"\"}";

		//param.put("data", data);
		//签名
		String SignatureParam = PosCommons.token+timeStamp+data;
		//logger.info("签名参数---"+SignatureParam);
		String Signature =PosMD5Util.MD5(SignatureParam);
		//logger.info("签名---"+Signature);
		param.put("data", data);
		param.put("Signature", Signature.toUpperCase());

		String posDeductionBalanceResult= PosHttpHelper.doPost(PosCommons.url, param);
		logger.info("--pos-增加用户余额-结果"+posDeductionBalanceResult);

		PosIncomeResult result=  JsonUtils.jsonToObject(PosIncomeResult.class, posDeductionBalanceResult);

		Integer code = result.ResCode;
		if(code!=0){
			throw new ErrorException(result.Msg);
		}
	}
	/**
	 *
	 * @param args
	 */
	 public static void main(String[] args) {
		 //67146439
	//	 33010000002 //绿卡
		// 33020000001 //黑卡

		// getPosMember("33010000002");//绿卡
		// getPosMember("33020000001");//黑卡
		 /*changePosScore("33010000002",new BigDecimal("0.03"),
				 score_transType_offset,"654321");*/
		 //addPosBalance("33010000002",new BigDecimal("0.02"),new BigDecimal("0.02"));
		// deductionPosBalance("33010000002",new BigDecimal("0.03"),"13456");
	}
  }
